#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter
from tkinter import *
import tkinter.filedialog  # 注意次数要将文件对话框导入

import hashlib
import time

from PIL import Image, ImageTk
from PIL.Image import Resampling

LOG_LINE_NUM = 0

class MY_GUI():
    def __init__(self,init_window_name):
        self.init_window_name = init_window_name


    #设置窗口
    def set_init_window(self):
        self.init_window_name.title("图片识别程序")           #窗口名
        #self.init_window_name.geometry('320x160+10+10')                         #290 160为窗口大小，+10 +10 定义窗口弹出时的默认展示位置
        self.init_window_name.geometry('1068x681+10+10')
        #self.init_window_name["bg"] = "pink"                                    #窗口背景色，其他背景色见：blog.csdn.net/chl0000/article/details/7657887
        #self.init_window_name.attributes("-alpha",0.9)                          #虚化，值越小虚化程度越高
        #标签
        self.init_data_label = Label(self.init_window_name, text="待处理图片")
        self.init_data_label.grid(row=0, column=0)
        self.result_data_label = Label(self.init_window_name, text="图片标签")
        self.result_data_label.grid(row=0, column=12)
        self.log_label = Label(self.init_window_name, text="日志")
        self.log_label.grid(row=12, column=0)


        # self.init_data_Text = Text(self.init_window_name, width=67, height=35)  #原始数据录入框
        # self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
        # 图片展示框
        self.canvas_width = 400
        self.canvas_height = 400

        pic_name = "./white.jpg"
        img_open = Image.open(pic_name)
        # 规范化图片的大小
        img_open = self.normalize(img_open)
        img_png = ImageTk.PhotoImage(img_open)

        self.init_data_Canvas = Label(self.init_window_name, bg='white', width=self.canvas_width, height=self.canvas_height,image=img_png)
        self.init_data_Canvas.img=img_png
        # self.init_data_Canvas = Label(self.init_window_name, bg='white')
        self.init_data_Canvas.grid(row=1, column=0,rowspan=10,columnspan=10)


        self.result_data_Text = Text(self.init_window_name, width=70, height=49)  #处理结果展示
        self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
        self.log_data_Text = Text(self.init_window_name, width=66, height=9)  # 日志框
        self.log_data_Text.grid(row=13, column=0, columnspan=10)
        #按钮
        self.photo_select_button = Button(self.init_window_name, text="图片选择", bg="lightblue", width=10,command=self.select_photo)  # 调用内部方法  加()为直接调用
        self.photo_select_button.grid(row=1, column=11)

        self.photo_process_button = Button(self.init_window_name, text="图片识别", bg="lightblue", width=10,
                                          command=self.add_text)  # 调用内部方法  加()为直接调用
        # 先预设一下，后面文本改为具体内容
        self.text_content="guaguazaici"
        self.photo_process_button.grid(row=2, column=11)

    # 规范化图片的大小
    def normalize(self,img):
        origin_weight = img.width
        origin_height = img.height
        height_scale = origin_height / self.canvas_height
        width_scale = origin_weight / self.canvas_width
        scale = height_scale if height_scale > width_scale else width_scale
        des_width = int(origin_weight / scale)
        des_height = int(origin_height / scale)
        img = img.resize((des_width, des_height), Resampling.LANCZOS)
        return img

    #选择图片
    def select_photo(self):
        filename = tkinter.filedialog.askopenfilename()
        self.filename = filename;
        # 后期加文件类型判断，读写差错判断
        img_open = Image.open(filename)
        img = self.normalize(img_open)
        img_png = ImageTk.PhotoImage(img)
        # 不知道可不可行，后期设置label的图像的函数有待确定
        # self.init_data_Canvas.create_image(0,0,image=img_png,anchor=W)
        self.init_data_Canvas.config(image=img_png)
        self.init_data_Canvas.image = img_png
        # self.init_data_Canvas.grid(row=1, column=0,rowspan=10,columnspan=10)
        self.write_log_to_Text("图片路径"+filename)

    # 暂行的处理函数，后期用完整的函数代替
    def add_text(self):
        self.result_data_Text.insert(END, self.text_content+self.filename+"\n")

    #获取当前时间
    def get_current_time(self):
        current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        return current_time


    #日志动态打印
    def write_log_to_Text(self,logmsg):
        global LOG_LINE_NUM
        current_time = self.get_current_time()
        logmsg_in = str(current_time) +" " + str(logmsg) + "\n"      #换行
        if LOG_LINE_NUM <= 7:
            self.log_data_Text.insert(END, logmsg_in)
            LOG_LINE_NUM = LOG_LINE_NUM + 1
        else:
            self.log_data_Text.delete(1.0,2.0)
            self.log_data_Text.insert(END, logmsg_in)


def gui_start():
    init_window = Tk()              #实例化出一个父窗口
    ZMJ_PORTAL = MY_GUI(init_window)
    # 设置根窗口默认属性
    ZMJ_PORTAL.set_init_window()

    init_window.mainloop()          #父窗口进入事件循环，可以理解为保持窗口运行，否则界面不展示


gui_start()